#include <stdio.h>
#include "iic_led.h"
#include "iic_led_stm32.h"

// 解用 IIC 读写函数
static inline int iic_led_read(void* self, char addr, int *data)
{
    return (*(IicLed_i**)self)->read_byte(self, addr, data);
}

static inline int iic_led_write(void* self, char addr, int data)
{
    return (*(IicLed_i**)self)->write_byte(self, addr, data);
}


int iic_led_on(IicLed *self)
{
    PRINT_FILE_LINE_FUNC();
    iic_led_write(self->iic_dev, self->reg_address, LIGHT_ON);
    return 0;
}

int iic_led_off(IicLed *self)
{
    PRINT_FILE_LINE_FUNC();
    iic_led_write(self->iic_dev, self->reg_address, LIGHT_OFF);
    return 0;
}

int iic_led_get_state(IicLed *self, int* state)
{
    PRINT_FILE_LINE_FUNC();
    return iic_led_read(self->iic_dev, self->reg_address, state);
}


Light_i iic_led_interface = {
    .on = (light_on_fn_t)iic_led_on, 
    .off = (light_off_fn_t)iic_led_off, 
    .get_state = (light_get_state_fn_t)iic_led_get_state
};

int iic_led_init(IicLed *self, IicLed_i** iic_dev)
{
    PRINT_FILE_LINE_FUNC();
    self->interface = &iic_led_interface;
    self->iic_dev = iic_dev;
    self->reg_address = IIC_LED_CTRL_REG;
    return 0;
}
